package com.NxIndustries.Sapphire;

import android.app.Activity;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.widget.FrameLayout;

import com.htc.configuration.HtcWrapConfiguration;
import com.htc.widget.ActionBarContainer;
import com.htc.widget.ActionBarExt;
import com.htc.widget.ActionBarText;

public class CarouselSettings extends Activity {
	public final static String AUTHORITY = "com.NxIndustries.Sapphire.CarouselSettings";

private Carousel mCarousel = null;
private ActionBarExt actionBarExt=null;
private ActionBarText actionBarText=null;    
private ActionBarContainer actionBarContainer=null;
private static final String AppTitle = "Welcome Sapphire User";
static Window window;
private Context context;
private boolean mFirstRun = true;

/*
 * Check whether the pro app is installed
 */
private boolean appInstalledOrNot(String uri) {
	PackageManager pm = getPackageManager();
	boolean app_installed = false;
	try {
		pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
		app_installed = true;
	} catch (PackageManager.NameNotFoundException e) {
		app_installed = false;
	}
	return app_installed;
}

/*
 * Set the initial launch
 */
public boolean isFirstRun() {
	return mFirstRun;
}

@Override
	protected void onCreate(Bundle savedInstanceState) {
                requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
		super.onCreate(savedInstanceState);
		
		boolean installed = appInstalledOrNot("com.NxIndustries.Sapphire1NE");
		if (installed) {
			System.out.println("Sapphire 1NE user detected!");
		} else {
			System.out.println("Sapphire services started...");
		}
		
		context = this;
		SetupActionBar();
		setTheme(getHtcThemeID(context, 1));

        final int rootId = 1;
        FrameLayout viewRoot = new FrameLayout(this);
        viewRoot.setId(rootId);
        setContentView(viewRoot);
        mCarousel = new Carousel();
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.add(rootId, mCarousel);
        ft.commit();
        
        registerForContextMenu(viewRoot);
        getWindow().setFormat(1);
	}

private void SetupActionBar() {
	actionBarExt=new ActionBarExt(this,getActionBar());
    actionBarContainer=actionBarExt.getCustomContainer();
    actionBarText=new ActionBarText(this);   
	actionBarText.setPrimaryText(AppTitle);
	actionBarContainer.addCenterView(actionBarText);
	boolean installed = appInstalledOrNot("com.NxIndustries.Sapphire1NE");
		if (installed) {
			actionBarText.setPrimaryText(AppTitle + " 1NE");
		} else {
			actionBarText.setPrimaryText(AppTitle);
		}
	}

public static int getHtcThemeID(Context context, int i)
{
	return HtcWrapConfiguration.getHtcThemeId(context, i);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
  MenuInflater inflater = getMenuInflater();
  inflater.inflate(R.menu.settings_menu, menu);
  return super.onCreateOptionsMenu(menu);
} 

@Override
public boolean onOptionsItemSelected(MenuItem item) {
  switch (item.getItemId()) {
  case R.id.one:
	@SuppressWarnings("unused")
	final Context context = this;
	Intent one = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=com.NxIndustries.Sapphire1NE"));
	startActivity(one);
		return true;
  case R.id.good:
		@SuppressWarnings("unused")
	final Context context1 = this;
		Intent good = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=com.NxIndustries.Sapphire"));
	startActivity(good);
}
return false;
}
}
